package edu.chalmers.trec;

public class Planet {
	public String name;
	public int food;
	public int energy;
	public int oxygen;
	public int population;

	public Planet(String name, int food, int energy, int oxygen, int population) {
		this.name = name;
		this.food = food;
		this.energy = energy;
		this.oxygen = oxygen;
		this.population = population;
	}

	public void update(float dt) {
		oxygen = Math.max(0, oxygen - population);
		food = Math.max(0, food - population);
		energy = Math.max(0, energy - (oxygen + food + population));

		if ((oxygen <= 0 || food <= 0) && population > 0) {
			population -= 1;
		}

		if (energy <= 0) {
			if (oxygen > 0) {
				oxygen -= 1;
			}
			if (food > 0) {
				food -= 1;
			}
		}
	}

	public String packValues() {
		return name + ":" + food + ":" + energy + ":" + oxygen + ":"
				+ population;
	}

	public void updateForPack(String pack) {
		String[] parse = pack.split(":");
		name = parse[0];
		food = Integer.parseInt(parse[1]);
		energy = Integer.parseInt(parse[2]);
		oxygen = Integer.parseInt(parse[3]);
		population = Integer.parseInt(parse[4]);
	}

	public String toString() {
		return "F:" + food + ", O:" + oxygen + ", E:" + energy + ", P:"
				+ population;
	}
}
